// ABC三个线程交替打印ABC
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
class PrintABC
{
public:
    PrintABC(int count) : count_(count) {}
    void printA()
    {
        for (int i = 0; i < count_; i++)
        {
            std::unique_lock<std::mutex> lock(mux_);
            cond_.wait(lock, [this]()
                       { return flag_ == 0; });
            std::cout << "A" << std::endl;
            flag_ = 1;
            cond_.notify_all();
        }
    }

    void printB()
    {
        for (int i = 0; i < count_; i++)
        {
            std::unique_lock<std::mutex> lock(mux_);
            cond_.wait(lock, [this]()
                       { return flag_ == 1; });
            std::cout << "B" << std::endl;
            flag_ = 2;
            cond_.notify_all();
        }
    }

    void printC()
    {
        for (int i = 0; i < count_; i++)
        {
            std::unique_lock<std::mutex> lock(mux_);
            cond_.wait(lock, [this]()
                       { return flag_ == 2; });
            std::cout << "C" << std::endl;
            flag_ = 0;
            cond_.notify_all();
        }
    }

private:
    int count_;
    std::mutex mux_;
    std::condition_variable cond_;
    int flag_=0;
};

int main()
{
    PrintABC printABC(10);
    std::thread t1(&PrintABC::printA,&printABC);
    std::thread t2(&PrintABC::printB,&printABC);
    std::thread t3(&PrintABC::printC,&printABC);
    t1.join();
    t2.join();
    t3.join();
    return 0;
}